#include "kernel_operator.h"
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;    
template<typename TYPE_Y> class KernelEye {
    using T = TYPE_Y;
public:
    __aicore__ inline KernelEye() {}
    __aicore__ inline void Init(GM_ADDR y,
                                uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum,
                                int32_t num_rows, int32_t num_columns, int32_t batch_shape_size, int32_t* batch_shape, int32_t dtype) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        this->num_rows = num_rows;
        this->num_columns = num_columns;
        this->batch_shape_size = batch_shape_size;
        this->dtype = dtype;
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));
        pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(float));
    }
    __aicore__ inline void Process() {
        if(this->batch_shape_size == 1)
        {
            for(int32_t k=0; k<this->batch_shape_size; k++)
            {
                for(int32_t i=0;i< this->num_rows;i++)
                {
                    yGm.SetValue(k*this->num_rows*this->num_columns+ i*this->num_columns+i,1);
                }
            }
        }
        else
        {
            for(int32_t k=0; k<this->batch_shape_size; k++)
            {
                for(int32_t i=0;i< this->num_rows;i++)
                {
                    for(int32_t j=0;j< this->num_columns;j++)
                    {
                        if(i == j)
                            yGm.SetValue(k*this->num_rows*this->num_columns+ i*this->num_columns+j,1);
                        else
                            yGm.SetValue(k*this->num_rows*this->num_columns+ i*this->num_columns+j,0);
                    }
                }
            }
        }
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmp2,QueueTmp3,QueueTmp4, QueueTmp5;

    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;

    int32_t num_rows;
    int32_t num_columns;
    int32_t batch_shape_size;
    int32_t batch_shape[20];
    int32_t dtype;
};
extern "C" __global__ __aicore__ void eye(GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    KernelEye<DTYPE_Y> op;
    op.Init(y,
            tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum,
            tiling_data.num_rows, tiling_data.num_columns, tiling_data.batch_shape_size, tiling_data.batch_shape, tiling_data.dtype);  
    op.Process();
}